//Pin Definitions
int rowA[] = {9,8,7,6,5,4,3,2};          //An Array defining which pin each row is attached to
                                         //(rows are common anode (drive HIGH))
int colA[] = {17,16,15,14,13,12,11,10};  //An Array defining which pin each column is attached to
                                         //(columns are common cathode (drive LOW))
int counter = 0;
                                         
void setup() {
  Serial.begin(9600); // for computer communication
  for(int i = 0; i <8; i++){  //Set the 16 pins used to control the array as OUTPUTs
    pinMode(rowA[i], OUTPUT);
    pinMode(colA[i], OUTPUT);
  }
}

void loop() {  
  
  if (Serial.available()) { // if serial data is present
    char c = (char)Serial.read();
    if (c == '1') { // tells arduino to increment the counter
      counter++;
    } else if (c == '0') {
      counter=0;
    }
  }
  
  int lightCount = counter % 64; // gives us a range of 0-63 which we can use to set the lights
  int rowListOnCount = lightCount / 8; // how many rows need to have all lights turned on
  int lastRowLightCount = lightCount % 8; // how many lights need to be switch on, on the row under `rowCount`
  
  for (int column=0; column<8; column++) {
    for (int row=0; row<8; row++) {
      digitalWrite(rowA[row], LOW); // turn off all row pins
    }
    
    for (int column2=0; column2<8; column2++) {
      if (column2==column) {
        digitalWrite(colA[column2], LOW); // turn on the current row
      } else {
        digitalWrite(colA[column2], HIGH); // turn off the current row
      }
    }
    
    for (int row=0; row<8; row++) {
      // if the row should have all lights turned on
      if (row < rowListOnCount) {
        digitalWrite(rowA[row], HIGH);
      } 
      // if the row needs a few lights turning on
      else if ( row == rowListOnCount && column < lastRowLightCount) {
        digitalWrite(rowA[row], HIGH);
      }
    }
    
    delay(1);
  }
  
  delay(1);
}
                                         

